﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common;

public partial class membership_login_dialog : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        Response.Expires = -1;

        if (RequestBase.GetFormString("otype") == "membership_login")
        {
            Login();
        }
    }
    #region 系统登录
    /// <summary>
    /// 系统登录
    /// </summary>
    void Login()
    {
        BLL.SQLIPrincipal principal = new BLL.SQLIPrincipal(RequestBase.GetFormString("txtUsername"), RequestBase.GetFormString("txtPassword"));

        if (!principal.Identity.IsAuthenticated)
        {
            BLL.AjaxHelper.ResponseWrite("用户名或密码错误,请重新输入!");
        }
        else
        {
            // 如果用户通过验证,则将用户信息保存在缓存中
            Context.User = principal;
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(RequestBase.GetFormString("txtUsername"), false, 1800);
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            CookieEncrypt.SetCookie(authCookie);

            if (Profile.AccountInfo != null)
            {
                Model.MembershipBase _obj = BLL.MembershipBase.GetObjByUsername(RequestBase.GetFormString("txtUsername"));
                Model.AccountBase account = new Model.AccountBase(_obj.FullName, "", "address1", "address2", _obj.RegionInfo, "", "zip", "", _obj.MobilePIN, _obj.Email, _obj.UserId, _obj.UserName);
                Profile.AccountInfo = account;
                Profile.Save();
            }
            BLL.AjaxHelper.ResponseWrite("succeeded");
        }
    }
    #endregion
}